package com.gton.service;

import com.gton.io.Resp;
import com.gton.util.ContextCommonMsg;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @description: 调取userManagerService
 * @author: GuoTong
 * @createTime: 2022-09-24 16:05
 * @since JDK 1.8 OR 11
 **/

@Service
@Slf4j
public class GotoUserManagerService {

    @Autowired
    private RestTemplate restTemplate;

    public Resp getUserManagerService_Hello() {
        String remoteServiceName = "user-manager";
        // rest基于Nacos的远程调用
        ResponseEntity<Resp> remoteCallBackEntity = restTemplate.
                getForEntity("http://" + remoteServiceName + "/hello",
                        Resp.class);

        HttpStatus statusCode = remoteCallBackEntity.getStatusCode();
        if (statusCode.isError()) {
            log.info("error state : " + remoteCallBackEntity.getStatusCodeValue());
            return Resp.error(ContextCommonMsg.FILE_CONNECTION_MSG, ContextCommonMsg.FILE_CONNECTION_CODE);
        }
        Resp callBackBody = remoteCallBackEntity.getBody();
        if (Objects.isNull(callBackBody)) {
            log.info("userId is error ");
            return Resp.error(null);
        }
        Object data = callBackBody.getData();
        return Resp.Ok(data);
    }

    public Resp<Map<String, Object>> createMapData() {
        Map<String, Object> data = new HashMap<>();
        data.put("call-UrI", "outSys_rest_to_this");
        data.put("port", "8099");
        data.put("service-Name", "cloud-nacos");
        data.put("server-type", "nacos");
        data.put("remote-method", "Dubbo");
        data.put("server-addr", "127.0.0.1:8848");
        data.put("swagger-Uri", "doc.html");
        return Resp.Ok(data);
    }

}
